local yixing = fk.CreateSkill {
  name = "th_yixing",
}

yixing:addEffect(fk.Damaged, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yixing.name) then
      local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if not use_event then return true end
      local use = use_event.data
      return not (use.extra_data and use.extra_data.th_yixing == player.id)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local prompt = Fk:translate("#th_yixing-invoke")
    if target ~= player and target:isAlive() then
      prompt = prompt .. Fk:translate("#th_yixing-to"):gsub("%%dest", Fk:translate(target.general))
    end
    return room:askToSkillInvoke(player, {
      skill_name = yixing.name,
      prompt = prompt
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, yixing.name)
    if player:isNude() or target == player or not target:isAlive() then return end
    local cid = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = yixing.name,
      cancelable = false,
      prompt = "#th_yixing-give::" .. target.id
    })
    if cid then
      local card = Fk:getCardById(cid[1])
      room:obtainCard(target, cid, false, fk.ReasonGive, player.id, yixing.name)
      if room:getCardOwner(cid[1]) == target then
        local use = room:askToUseRealCard(target, {
          pattern = cid,
          skill_name = yixing.name,
          prompt = "#th_yixing-use:::" .. card:toLogString(),
          skip = true
        })
        if use then
          use.extra_data = use.extra_data or {}
          use.extra_data.th_yixing = player.id
          room:useCard(use)
        else
          room:throwCard(cid, yixing.name, target, target)
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["th_yixing"] = "移形",
  [":th_yixing"] = "一名角色不以此法受到伤害后，你可以摸一张牌，若不为你，你须交给其一张牌，其须使用或弃置之。",

  ["#th_yixing-invoke"] = "移形：你可以摸一张牌",
  ["#th_yixing-to"] = "并交给 %dest 一张牌，若此牌可使用，其需使用之",

  ["#th_yixing-give"] = "移形：你须交给 %dest 一张牌",
  ["#th_yixing-use"] = "移形：你需使用 %arg ，点“取消”则弃置之",
}

return yixing